package com.agileai.portal.controller.mobile;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONObject;

import com.agileai.domain.DataParam;
import com.agileai.domain.DataRow;
import com.agileai.hotweb.annotation.PageAction;
import com.agileai.hotweb.controller.core.SimpleHandler;
import com.agileai.hotweb.domain.core.Profile;
import com.agileai.hotweb.domain.core.Resource;
import com.agileai.hotweb.domain.core.User;
import com.agileai.hotweb.renders.AjaxRenderer;
import com.agileai.hotweb.renders.ViewRenderer;
import com.agileai.portal.bizmoduler.wcm.InfomationFavEditManage;
import com.agileai.portal.bizmoduler.wcm.InfomationManage;
import com.agileai.util.DateUtil;

public class MobileInformationsDataProviderHandler extends SimpleHandler {

	public MobileInformationsDataProviderHandler(){
		super();
	}
	private static String Y="Y";
	private static String N="N";
	@PageAction
	public ViewRenderer findKnowledgeInformations(DataParam param){
		String responseText = null;
		try {
			JSONObject jsonObject = new JSONObject();
			InfomationManage infomationManage = this.lookupService(InfomationManage.class);
			//公司动态
			String colCode1 = "corp-news";
			List<DataRow> results1 = infomationManage.getRightNews(colCode1);
			JSONArray jsonArray1 = new JSONArray();
			for(int i=0;i<results1.size();i++){
				if(i>20){
					break;
				}
				DataRow row = results1.get(i);
				JSONObject jsonObject1 = new JSONObject();
				jsonObject1.put("title", row.stringValue("INFO_TITLE"));
				jsonObject1.put("id", row.stringValue("INFO_ID"));
				jsonObject1.put("outLine", row.stringValue("INFO_OUTLINE"));
				jsonObject1.put("readCounts", String.valueOf(row.getInt("INFO_READ_COUNT")));
				jsonArray1.put(jsonObject1);
			}
			jsonObject.put("corpNews", jsonArray1);
			//业界资讯
			String colCode2 = "industry-news";
			List<DataRow> results2 = infomationManage.getRightNews(colCode2);
			JSONArray jsonArray2 = new JSONArray();
			for(int i=0;i<results2.size();i++){
				if(i>20){
					break;
				}
				DataRow row = results2.get(i);
				JSONObject jsonObject2 = new JSONObject();
				jsonObject2.put("title", row.stringValue("INFO_TITLE"));
				jsonObject2.put("id", row.stringValue("INFO_ID"));
				jsonObject2.put("outLine", row.stringValue("INFO_OUTLINE"));
				jsonObject2.put("readCounts", String.valueOf(row.getInt("INFO_READ_COUNT")));
				jsonArray2.put(jsonObject2);
			}
			jsonObject.put("industryNews", jsonArray2);
			//技术集锦
			String colCode3 = "tech-trends";
			List<DataRow> results3 = infomationManage.getRightNews(colCode3);
			JSONArray jsonArray3 = new JSONArray();
			for(int i=0;i<results3.size();i++){
				if(i>20){
					break;
				}
				DataRow row = results3.get(i);
				JSONObject jsonObject3 = new JSONObject();
				jsonObject3.put("title", row.stringValue("INFO_TITLE"));
				jsonObject3.put("id", row.stringValue("INFO_ID"));
				jsonObject3.put("outLine", row.stringValue("INFO_OUTLINE"));
				jsonObject3.put("readCounts", String.valueOf(row.getInt("INFO_READ_COUNT")));
				jsonArray3.put(jsonObject3);
			}
			jsonObject.put("techTrends", jsonArray3);
			//培训中心
			String colCode4 = "traincenter";
			List<DataRow> tempRecords = infomationManage.getRightNews(colCode4);
			List<DataRow> results4 = this.getAuthedRecords(tempRecords);
			JSONArray jsonArray4 = new JSONArray();
			for(int i=0;i<results4.size();i++){
				if(i>20){
					break;
				}
				DataRow row = results4.get(i);
				JSONObject jsonObject4 = new JSONObject();
				jsonObject4.put("title", row.stringValue("INFO_TITLE"));
				jsonObject4.put("id", row.stringValue("INFO_ID"));
				jsonObject4.put("outLine", row.stringValue("INFO_OUTLINE"));
				jsonObject4.put("readCounts", String.valueOf(row.getInt("INFO_READ_COUNT")));
				jsonArray4.put(jsonObject4);
			}
			jsonObject.put("traincenter", jsonArray4);
			responseText = jsonObject.toString();
		} catch (Exception e) {
			log.error(e.getLocalizedMessage(), e);
		}
		return new AjaxRenderer(responseText);
	}
	
	private List<DataRow> getAuthedRecords(List<DataRow> tempRecords){
		List<DataRow> result = new ArrayList<DataRow>();
		Profile profile = this.getProfile();
		if (profile != null){
			User user = (User)profile.getUser();
			for (int i=0;i < tempRecords.size();i++){
				DataRow row = tempRecords.get(i);
				String authType = row.stringValue("INFO_AUTH_TYPE");
				if (InfomationManage.AuthType.Private.equalsIgnoreCase(authType)){
					String infomatonId = row.stringValue("INFO_ID");
					if (user.containResouce(Resource.Type.Infomation, infomatonId)
							|| "user".equals(user.getUserCode()) || "admin".equals(user.getUserCode())){
						result.add(row);
					}
				}else{
					result.add(row);
				}
			}
		}else{
			for (int i=0;i < tempRecords.size();i++){
				DataRow row = tempRecords.get(i);
				String authType = row.stringValue("INFO_AUTH_TYPE");
				if (InfomationManage.AuthType.Public.equalsIgnoreCase(authType)){
					result.add(row);
				}
			}
		}
		return result;
	}
	
	@PageAction
	public ViewRenderer getKnowledgeInformation(DataParam param){
		String responseText = null;
		try {
			String infoId = param.get("ID");
			User user = (User) this.getUser();
			String userCode = user.getUserCode();
			InfomationManage infomationManage = this.lookupService(InfomationManage.class);
			DataRow record = infomationManage.getContentRecord(infoId);
			String id = record.getString("INFO_ID");
			String title = record.getString("INFO_TITLE");
			String content = record.getString("INFO_MOBILE_CONTENT");
			Date date = (Date) record.get("INFO_CREATE_TIME");
			String createTime = DateUtil.getDateByType(DateUtil.YYMMDDHHMI_HORIZONTAL,date);
			List<DataRow> records = infomationManage.findPraiseRecords(infoId, userCode);
			InfomationFavEditManage infomationFavEditManage = this.lookupService(InfomationFavEditManage.class);
			param.put("userCode", userCode,"infoId", infoId);
			List<DataRow> favRecords = infomationFavEditManage.findInfoRecords(param);
			String isPraised = N;
			if(records.size()>0){
				isPraised = Y;
			}
			String isFavorited = N;
			if(favRecords.size()>0){
				isFavorited = Y;
			}
			JSONObject jsonObject = new JSONObject();
			jsonObject.put("id", id);
			jsonObject.put("title", title);
			jsonObject.put("createTime", createTime);
			jsonObject.put("isPraised", isPraised);
			jsonObject.put("isFavorited", isFavorited);
			jsonObject.put("content", content);
			responseText = jsonObject.toString();
		} catch (Exception e) {
			log.error(e.getLocalizedMessage(), e);
		}
		return new AjaxRenderer(responseText);
	}
}
